﻿using Comm100.Framework.Domain.Repository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Bot.Domain.Entities;
using Bot.Domain.Interfaces;
using Bot.Domain.Bo;
using Castle.Windsor;
using Bot.Domain.Specifications;
using AutoMapper;

namespace Bot.Domain.Categories.Service
{
    public class GreetingMessageInChannelDomainService : IGreetingMessageInChannelDomainService
    {
        private IWindsorContainer _container;
        private readonly IRepository<Guid, GreetingMessageInChannel> _repository;
        public IMapper Mapper { get; set; }
        public GreetingMessageInChannelDomainService(IRepository<Guid, GreetingMessageInChannel> repository, IWindsorContainer container)
        {
            this._repository = repository;
            this._container = container;
        }

        public GreetingMessageInChannel Create(GreetingMessageInChannel greeting)
        {
            if(greeting.Id == Guid.Empty)
            {
                greeting.Id = Guid.NewGuid();
            }

            var result= _repository.Create(greeting);
            if (greeting.Responses != null && greeting.Responses.Count> 0)
            {
                IResponseDomainService responseDomainService = _container.Resolve<IResponseDomainService>();
                foreach (var item in greeting.Responses)
                {
                    item.ParentId = greeting.Id;
                    responseDomainService.Create(item);
                }
            }            
            return GetById(result.Id);
        }

        public GreetingMessageInChannel Get(Guid id, string include = "")
        {
            var greetingMessageInChannel = _repository.Get(id);
            if (!string.IsNullOrEmpty(include) && include.Split(',').Contains("responses"))
            {
                IResponseDomainService responseDomainService = _container.Resolve<IResponseDomainService>();
                var responses = responseDomainService.GetByParentId(greetingMessageInChannel.Id, include);
                greetingMessageInChannel.Responses = responses.ToList();
            }
            return greetingMessageInChannel;
        }

        public GreetingMessageInChannel Update(GreetingMessageInChannelUpdateBo bo)
        {
            GreetingMessageInChannel greetingMessageInChannel = _repository.Get(bo.Id);
            //greetingMessageInChannel.Id = bo.Id;
            //greetingMessageInChannel.BotId = bo.BotId;
            greetingMessageInChannel.Channel = bo.Channel;

            if (bo.Responses != null && bo.Responses.Count > 0)
            {
                IResponseDomainService responseDomainService = _container.Resolve<IResponseDomainService>();
                List<Guid> ids = new List<Guid> { };

                foreach (var item in bo.Responses)
                {
                    if (item.Id != Guid.Empty)
                    {
                        //修改      
                        item.ParentId = bo.Id;
                        responseDomainService.Update(item);
                        ids.Add(item.Id);
                    }
                    else
                    {
                        //新增
                        Response response = Mapper.Map<Response>(item);
                        response.Id = Guid.NewGuid();
                        response.ParentId = bo.Id;
                        responseDomainService.Create(response);
                        ids.Add(response.Id);
                    }
                }
                //删除
                var resItems = responseDomainService.GetByParentId(bo.Id);
                foreach (var q in resItems)
                {
                    if (!ids.Contains(q.Id))
                    {
                        responseDomainService.Delete(q.Id);
                    }
                }      
            }

            _repository.Update(greetingMessageInChannel);

            return greetingMessageInChannel;

        }

        public IEnumerable<GreetingMessageInChannel> List()
        {
            return _repository.ListAll();
        }

        public void Delete(Guid id)
        {
            GreetingMessageInChannel greetingMessageInChannel = Get(id, "responses");
            if(greetingMessageInChannel!=null
                && greetingMessageInChannel.Responses != null)
            {
                IResponseDomainService responseDomainService = _container.Resolve<IResponseDomainService>();
                foreach (var response in greetingMessageInChannel.Responses)
                {
                    responseDomainService.Delete(response.Id);
                }
            }

            _repository.Delete(greetingMessageInChannel);

        }

        public List<GreetingMessageInChannel> GetByBotId(Guid botId, string include = "")
        {
            var greetingMessageInChannels = _repository.List(new GreetingMessageInChannelFilterSpecification(botId)).ToList();
            if (greetingMessageInChannels != null
                && greetingMessageInChannels.Count > 0
                && !string.IsNullOrEmpty(include)
                && include.Split(',').Contains("responses"))
            {
                IResponseDomainService responseDomainService = _container.Resolve<IResponseDomainService>();
                foreach (var greetingMessageInChannel in greetingMessageInChannels)
                {
                    var responses = responseDomainService.GetByParentId(greetingMessageInChannel.Id, include);
                    greetingMessageInChannel.Responses = responses.ToList();
                }
            }
            return greetingMessageInChannels;
        }

        public GreetingMessageInChannel GetById(Guid id, string include="")
        {
            var result = _repository.Get(id);
            IResponseDomainService responseDomainService = _container.Resolve<IResponseDomainService>();
            result.Responses = responseDomainService.GetByParentId(id, include).ToList();
            return result;
        }
        
        public GreetingMessageInChannel GetByBotIdAndChannel(Guid botId, string channel)
        {
            var greetingMessageInChannels = _repository.List(new GreetingMessageInChannelFilterSpecification(botId,channel)).ToList();
            return greetingMessageInChannels.FirstOrDefault();
        }
    }
}
